<?php


namespace app\admin\controller\card;

use app\common\controller\Backend;
use app\common\model\card\Kabawlogtype;

/**
 * 划拨与记录
 *
 * @icon fa fa-circle-o
 */
class Appropriate extends Backend
{

    /**
     * Transferlog模型对象
     * @var \app\common\model\card\entity\Transferlog
     */
    protected $model = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\card\Entity;
        $this->view->assign("statusList", $this->model->getStatusList());
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['entitysite'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $logtype=Kabawlogtype::field("card_type_name,number,price")->where(["card_kabaw_id"=>$row["entitysite"]["card_kabaw_id"]])->select();
                $logtype_str="";
                if(!empty($logtype)){
                    foreach ($logtype as $k=>$vs){
                        if(empty($k)){
                            $logtype_str=$vs["card_type_name"]."(".$vs["number"]."次)";
                        }else{
                            $logtype_str.="\n".$vs["card_type_name"]."(".$vs["number"]."次)";
                        }
                    }
                }
                $row["logtype"]=$logtype_str;
                $row["residue"]=$row["number"]-$row["transfer_num"];
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
